package com.javandad.app.client.profile;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.javandad.app.shared.entity.User;

public class ProfileWidget extends FlexTable {
	private User user;
	private TextEdit firstNameEdit;
	private TextEdit lastNameEdit;
	private TextEdit emailEdit;
	private TextEdit cellNoEdit;
	private TextEdit countryEdit;
	private TextEdit cityEdit;
	private TextEdit postalAddressEdit;
	private TextEdit phoneNoEdit;
	private List<TextEdit> allTheTextEdits;
	private Button editButton;
	
	private ChangeHandler phoneChecker = new ChangeHandler() {
		public void onChange(ChangeEvent event) {
			TextBox edit = (TextBox)event.getSource();
			String phoneNo = edit.getText();
			if (!phoneNo.matches("^[0-9 +\\-]*$"))
			{
				Window.alert(phoneNo+" is not a valid phone number!");
				edit.selectAll();
				edit.setFocus(true);
			}
		}};

	public ProfileWidget(User init, boolean viewable, boolean editable) {
		int row = 0;

		this.setText(row, 0, "First name:");
		firstNameEdit = new TextEdit(!viewable);
		this.setWidget(row, 1, firstNameEdit);
		row++;
		
		this.setText(row, 0, "Last name:");
		lastNameEdit = new TextEdit(!viewable);
		this.setWidget(row, 1, lastNameEdit);
		row++;
		
		this.setText(row, 0, "Email address:");
		emailEdit = new TextEdit(!viewable);
		this.setWidget(row, 1, emailEdit);
		row++;

		this.setText(row, 0, "Cell phone number:");
		cellNoEdit = new TextEdit(!viewable);
		this.setWidget(row, 1, cellNoEdit);
		cellNoEdit.edit.addChangeHandler(phoneChecker);
		row++;

		this.setText(row, 0, "Land line number:");
		phoneNoEdit = new TextEdit(!viewable);
		this.setWidget(row, 1, phoneNoEdit);
		phoneNoEdit.edit.addChangeHandler(phoneChecker);
		row++;

		this.setText(row, 0, "Country:");
		countryEdit = new TextEdit(!viewable);
		this.setWidget(row, 1, countryEdit);
		row++;

		this.setText(row, 0, "City:");
		cityEdit = new TextEdit(!viewable);
		this.setWidget(row, 1, cityEdit);
		row++;

		this.setText(row, 0, "Postal address:");
		postalAddressEdit = new TextEdit(!viewable);
		this.setWidget(row, 1, postalAddressEdit);
		row++;
		
		allTheTextEdits = new LinkedList<TextEdit>();
		allTheTextEdits.add(firstNameEdit);
		allTheTextEdits.add(lastNameEdit);
		allTheTextEdits.add(emailEdit);
		allTheTextEdits.add(cellNoEdit);
		allTheTextEdits.add(phoneNoEdit);
		allTheTextEdits.add(countryEdit);
		allTheTextEdits.add(cityEdit);
		allTheTextEdits.add(postalAddressEdit);
		
		if(viewable && editable)
		{
			editButton = new Button();
			editButton.setText("edit");
			this.setWidget(row, 1, editButton);
			editButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					boolean editable = editButton.getText().equals("edit");
					setEditable(editable);
				}});
			row++;
		}
		
		setUser(init);
	}
	
	public User getUser() 
	{
		return user;
	}
	
	public void updateUserData()
	{
		updateUser();
		for(UserChangeHandler h : handlers)
			h.userChanged(user);
	}
	
	public void setUser(User user)
	{
		this.user = user;
		updateTextEdits();
	}
	protected void updateTextEdits()
	{
		firstNameEdit.setText(user.getFirstName());
		lastNameEdit.setText(user.getLastName());
		emailEdit.setText(user.getEmail());
		cellNoEdit.setText(user.getCellNo());
		countryEdit.setText(user.getCountry());
		cityEdit.setText(user.getCity());
		postalAddressEdit.setText(user.getPostalAddress());
		phoneNoEdit.setText(user.getPhoneNo());
	}
	
	protected void updateUser()
	{
		updateUser(user);
	}
	protected void updateUser(User user)
	{
		user.setFirstName(firstNameEdit.getText());
		user.setLastName(lastNameEdit.getText());
		user.setEmail(emailEdit.getText());
		user.setCellNo(cellNoEdit.getText());
		user.setCountry(countryEdit.getText());
		user.setCity(cityEdit.getText());
		user.setPostalAddress(postalAddressEdit.getText());
		user.setPhoneNo(phoneNoEdit.getText());
	}
	
	public interface UserChangeHandler
	{
		public void userChanged(User user);
	}
	protected List<UserChangeHandler> handlers = new LinkedList<UserChangeHandler>();
	public void addUserChangeHandler(UserChangeHandler h)
	{
		handlers.add(h);
	}
	
	public void setEditable(boolean editable)
	{
		for(TextEdit e : allTheTextEdits)
			e.setEditable(editable);
		editButton.setText(editable ? "done" : "edit");
		if(!editable)
			updateUserData();
	}
}
